﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Xml;
using Axis.Web.Administrator.Global;
using Axis.Dao.Research;
using Axis.BusinessObject.Core;

namespace Axis.Web.Views.Accounts.Service
{
    /// <summary>
    /// Summary description for AccountService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class AccountService : System.Web.Services.WebService
    {

        [WebMethod(EnableSession = true)]
        public string MemberRegister(string pEmail, string pFirstName, string pLastName, string pPassword, string pDateOfBirth, string pGender, string pCaptcha)
        {
            string serverCaptcha = "";
            if (Session["CAPTCHA_CODE"] != null)
                serverCaptcha = Session["CAPTCHA_CODE"].ToString();
            string result = new AccountUI().MemberRegister(pEmail, pFirstName, pLastName, pPassword, pDateOfBirth, pGender, pCaptcha, ref serverCaptcha);
            Session["CAPTCHA_CODE"] = serverCaptcha;
            return result;
        }
        [WebMethod(EnableSession = true)]
        public string Login(string pEmail, string pPassword, string pIsRemember)
        {
            CMemberList member = new CMemberList();
            string result = new AccountUI().Login(pEmail, pPassword, pIsRemember, out member);
            Session["MEMBERINFO"] = member;
            return result;


        }
        [WebMethod(EnableSession = true)]
        public string CheckSession()
        {
            return new AccountUI().CheckSession();
        }
        [WebMethod(EnableSession = true)]
        public string Logout()
        {
            if (Session["MEMBERINFO"] != null)
                Session.Remove("MEMBERINFO");
            HttpContext.Current.Session.Abandon();
            HttpContext.Current.Session.Clear();
            foreach (string key in HttpContext.Current.Response.Cookies.AllKeys)
            {
                HttpCookie cookie = new HttpCookie(key);
                cookie.Expires = DateTime.UtcNow.AddDays(-1);
                HttpContext.Current.Response.Cookies.Remove(key);
            }

            Session["MEMBERINFO"] = null;
            Session.Clear();
            Session.Abandon();
            return "true";
        }
        [WebMethod(EnableSession = true)]
        public string CheckLogin()
        {
            var member = new CMemberList();
            if (Session["MEMBERINFO"] != null)
                member = Session["MEMBERINFO"] as CMemberList;
            return new AccountUI().CheckLogin(member);
        }
        [WebMethod(EnableSession = true)]
        public string CheckEmail(string pEmail)
        {
            return new AccountUI().CheckEmail(pEmail);
        }
        [WebMethod(EnableSession = true)]
        public string UpdateMember(string pInputValue)
        {
            var member = new CMemberList();
            if (Session["MEMBERINFO"] != null)
                member = Session["MEMBERINFO"] as CMemberList;
            var member2 = new CMemberList();
            string result = new AccountUI().UpdateMember(pInputValue, member, out member2);
            Session["MEMBERINFO"]=member2;
            return result;
        }
        [WebMethod(EnableSession = true)]
        public string GetMember(string pInputValue)
        {
            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("InputValue");

            var member = new CMemberList();
            if (Session["MEMBERINFO"] != null)
                member = Session["MEMBERINFO"] as CMemberList;

            doc.AppendChild(root);
            // root.SetAttribute("UserID", CSecurity.MEMBER_ID.ToString());
            root.SetAttribute("ID", member.ID.ToString());
            root.SetAttribute("UserID", member.ID.ToString());
            root.SetAttribute("SecrectToken", member.SecrectToken);
            root.SetAttribute("Version", "2");
            var list = new AccountUI().GetMember(root.OuterXml);
            if (list != null)
                return new JavaScriptSerializer().Serialize(list);
            return new JavaScriptSerializer().Serialize(root.OuterXml);
        }
        [WebMethod(EnableSession = true)]
        public string ForgotPassword(string pEmail, string pCaptcha)
        {

            var member = new CMemberList();
            if (Session["MEMBERINFO"] != null)
                member = Session["MEMBERINFO"] as CMemberList;
            string sessionCaptcha="";
            if (Session["CAPTCHA_CODE"] != null)
                sessionCaptcha = Session["CAPTCHA_CODE"].ToString();
            return new AccountUI(member).ForgotPassword(pEmail, pCaptcha,ref sessionCaptcha);

        }
        [WebMethod(EnableSession = true)]
        public string ChangeMarkToGift(string ID)
        {

            var member = new CMemberList();
            if (Session["MEMBERINFO"] != null)
                member = Session["MEMBERINFO"] as CMemberList;
            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("InputValue");
            doc.AppendChild(root);
            // root.SetAttribute("UserID", CSecurity.MEMBER_ID.ToString());
            root.SetAttribute("GiftID", ID);
            root.SetAttribute("Action", "ConvertMarkToGift");
            root.SetAttribute("UserID", member.ID.ToString());
            root.SetAttribute("SecrectToken", member.SecrectToken);
            return new JavaScriptSerializer().Serialize(new CResearchDao().ActionMemberGift(doc.OuterXml));
        }
    }
}
